<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\goods\admin;

use app\goods\model\Product as ProductModel;
use app\goods\model\Brand as BrandModel;
use app\goods\model\Crowd as CrowdModel;
use app\goods\model\ColorGroup as ColorGroupModel;
use app\goods\model\Color as ColorModel;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Cache;
use think\Db;

/**
 * 商品管理
 * @package app\admin\controller
 */
class Product extends Admin
{
    /**
    *商品列表
    */
    public function index(){
    	$data_list = ProductModel::goodsSelect();     
        return ZBuilder::make('table')
        ->addColumns([ // 批量添加列
            ['GoodsID', 'ID'],
            ['GoodsNo', '编码'],
            ['GoodsName', '名称'],
            ['OtherName', '别名'],
            ['ClassName', '款类'],
            ['BrandName', '品牌'],
            ['CrowdName', '人群'],
            ['UnitName', '单位'],
            ['ColorGroupName', '色系'],
            ['ColorName', '颜色'],
            ['SizeGroupName', '尺码系'],
            ['SizeName', '尺码'],
            ['update_time', '修改时间', 'datetime'],
            ['create_time', '创建时间', 'datetime'],
            ['right_button', '操作', 'btn']
        ])
        ->addRightButton('edit')
        ->addTopButton('add')
        ->setRowList($data_list)
        ->fetch();
    }


    /**
    *新增
    */

    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = $this->validate($data, 'Product');
            if(true !== $result) return $this->error($result);
            $result = ProductModel::insert($data);
            if($result) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }

        //品牌列表
        $brand_list = BrandModel::where('Status', '1')->column('BrandID,BrandName');

        //人群列表
        $crowd_list = CrowdModel::where('Status', '1')->column('CrowdID,CrowdName');

        //款号父类
        $classGroup_list = Db::name('goods_class')->where(array('Status' => '1','ParentID' => '0'))->column('ClassID,ClassName');

        //色系
        $colorGroup_list = ColorGroupModel::where('status', '1')->column('ColorGroupID,GroupName');

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列            
            ['text', 'GoodsName', '名称'],
            ['text', 'OtherName', '别名'],
            ['select', 'BrandID', '品牌', '<span class="text-danger">必选</span>', $brand_list],
            ['select', 'CrowdID', '人群', '<span class="text-danger">必选</span>', $crowd_list],
            ['Linkage', 'ClassGroupID', '款类', '<span class="text-danger">必选</span>', $classGroup_list, '', url('get_class'), 'ClassID'],
            ['select', 'ClassID', '款号', '<span class="text-danger">必选</span>'],
            ['Linkage', 'ColorGroupID', '色系', '<span class="text-danger">必选</span>', $colorGroup_list, '', url('get_color'), 'ColorID'],
            ['select', 'ColorID', '颜色', '<span class="text-danger">必选</span>'],          
            
        ])
        ->layout(['GoodsName' => 6, 'OtherName' => 6, 'BrandID' => 6, 'CrowdID' => 6, 'ClassGroupID' => 6, 'ClassID' => 6, 'ColorGroupID' => 6, 'ColorID' => 6])
        ->fetch();
    }


    /**
    *获取款号
    */
    public function get_class(){
        $id = input('ClassGroupID');
        $data = Db::name('goods_class')->where(array('Status' => '1','ParentID' => $id))->column('ClassID,ClassName');
        $list = [];
        foreach ($data as $key => $value) {
            $list[]=['key' => $key, 'value' => $value];
        }
        $data_list['list'] = $list;
        $data_list['code'] = '1'; //判断状态
        $data_list['msg']  = '请求成功'; //回传信息
        return json($data_list);
    }


    /**
    *获取颜色
    */
    public function get_color(){
        $id = input('ColorGroupID');
        $data = ColorModel::where(array('Status' => '1','ColorGroupID' => $id))->column('ColorID,Name');
        $list = [];
        foreach ($data as $key => $value) {
            $list[]=['key' => $key, 'value' => $value];
        }
        $data_list['list'] = $list;
        $data_list['code'] = '1'; //判断状态
        $data_list['msg']  = '请求成功'; //回传信息
        return json($data_list);
    }
}
